<template>
  <div class="tree-item">
    <span class="label">{{ item.name }}</span>
    <span class="people">{{ item.manager }}</span>
    <el-dropdown @command="command">
      <span class="el-dropdown-link">
        操作<i class="el-icon-arrow-down el-icon--right" />
      </span>
      <template #dropdown>
        <el-dropdown-menu>
          <el-dropdown-item command="add">添加子部门</el-dropdown-item>
          <el-dropdown-item v-if="!item.isTop" command="look">
            查看部门
          </el-dropdown-item>
          <el-dropdown-item v-if="!item.isTop" command="del">
            删除部门
          </el-dropdown-item>
        </el-dropdown-menu>
      </template>
    </el-dropdown>
  </div>
</template>

<script>
import { companyDepartmentDel } from '@/api/departments.js'
export default {
  name: 'TreeItem',
  props: ['item'],
  methods: {
    command(command) {
      switch (command) {
        case 'add':
          this.$bus.$emit('show', true, this.item)
          break
        case 'look':
          this.$bus.$emit('show', true, this.item, 'edit')
          break
        case 'del':
          this.$confirm('您确定要删除该部门吗？', '温馨提示').then(async() => {
            await companyDepartmentDel(this.item.id)
            this.$emit('getDepartment')
            this.$message.success('删除部门成功')
          })
          break
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.tree-item {
  width: 100%;
  display: flex;
  font-size: 14px;
  .label {
    flex: 1;
    font-weight: 700;
  }
  .people {
    margin-right: 30px;
  }
}
</style>
